/*
 * @Description: 控制台日志
 * @Author: DHL
 * @Date: 2022-04-20 10:45:16
 * @LastEditors: DHL
 * @LastEditTime: 2022-04-20 11:17:05
 */

/**
 * 日志打印
 */
class Log {
  type = ['primary', 'success', 'warn', 'error', 'info']

  typeColor(type: string) {
    let color = ''
    switch (type) {
      case 'primary':
        color = '#2d8cf0'
        break
      case 'success':
        color = '#19be6b'
        break
      case 'info':
        color = '#909399'
        break
      case 'warn':
        color = '#ff9900'
        break
      case 'error':
        color = '#f03f14'
        break
      default:
        color = '#35495E'
        break
    }
    return color
  }

  isArray(obj: any) {
    return Object.prototype.toString.call(obj) === '[object Array]'
  }

  /**
   * 美化样式
   */
  pretty(type = 'primary', title: string, text: string) {
    if (typeof text === 'object') {
      console.group('Console Group', title)
      console.log(
        `%c ${title}`,
        `background:${this.typeColor(type)};border:1px solid ${this.typeColor(type)};
            padding: 1px; border-radius: 4px; color: #fff;`
      )
      this.isArray(text) ? console.table(text) : console.dir(text)

      console.groupEnd()
      return
    }
    console.log(
      `%c ${title} %c ${text} %c`,
      `background:${this.typeColor(type)};border:1px solid ${this.typeColor(type)};
          padding: 1px; border-radius: 4px 0 0 4px; color: #fff;`,
      `border:1px solid ${this.typeColor(type)};
          padding: 1px; border-radius: 0 4px 4px 0; color: ${this.typeColor(type)};`,
      'background:transparent'
    )
  }

  /**
   * 主打印
   * @param title
   * @param text
   */
  primary(title: string, ...text: any[]) {
    text.forEach((t) => this.pretty('primary', title, t))
  }

  /**
   * 成功打印
   * @param title
   * @param text
   */
  success(title: string, ...text: any[]) {
    text.forEach((t) => this.pretty('success', title, t))
  }

  /**
   * 警告打印
   * @param title
   * @param text
   */
  warn(title: string, ...text: any[]) {
    text.forEach((t) => this.pretty('warn', title, t))
  }

  /**
   * 错误打印
   * @param title
   * @param text
   */
  error(title: string, ...text: any[]) {
    text.forEach((t) => this.pretty('error', title, t))
  }

  /**
   * 信息打印
   * @param title
   * @param text
   */
  info(title: string, ...text: any[]) {
    text.forEach((t) => this.pretty('info', title, t))
  }
}

export const log = new Log()
